<script setup lang="ts">
import { ref } from "vue";
import { useStudent } from "./hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import AddFill from "@iconify-icons/ri/add-circle-line";

const AddStudentToRef = ref();
const UpdateStudentToRef = ref();
const formRef = ref();
const {
  form,
  rules,
  sex,
  loading,
  columns,
  dataList,
  pagination,
  addStudentDto,
  updateStudentDto,
  updatePanelVisible,
  addPanelVisible,
  classList,
  delegateTeamList,
  onSearch,
  addStudent,
  changePasswordType,
  CloseAddPanel,
  ShowAddStudentPanel,
  updateStudent,
  ShowUpdateStudentPanel,
  CloseUpdatePanel,
  ClassOnChange,
  handleDelete,
  handleSizeChange,
  handleCurrentChange
} = useStudent();
</script>

<template>
  <div class="main">
    <el-form ref="formRef" :inline="true" :model="form" class="bg-bg_color w-[99/100] pl-4 pt-4">
      <el-form-item label="班级名称" prop="ClassId">
        <el-select placeholder="请选择" filterable v-model="form.ClassId" @change="ClassOnChange">
          <el-option v-for="item in classList" :key="item.key" :label="item.value" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="团队名称" prop="DelegateTeamId">
        <el-select placeholder="请选择" filterable clearable v-model="form.DelegateTeamId">
          <el-option v-for="item in delegateTeamList" :key="item.key" :label="item.value" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item><el-button type="primary" @click="onSearch()">搜索</el-button> </el-form-item>
    </el-form>

    <el-dialog v-model="addPanelVisible" title="批量添加" :close-on-click-modal="false"
      @close="CloseAddPanel(AddStudentToRef)" width="30%" center>
      <el-form ref="AddStudentToRef" label-position="left" label-width="100px" :rules="rules" :model="addStudentDto"
        style="max-width: 460px">
        <el-form-item label="班级名称" prop="ClassId">
          <el-select placeholder="请选择" filterable v-model="addStudentDto.ClassId" class="w-220">
            <el-option v-for="item in classList" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="代表队数量" prop="DelegateTeamNum">
          <el-input-number v-model="addStudentDto.DelegateTeamNum" :min="1" :max="10" />
        </el-form-item>
        <el-form-item label="密码:" prop="PasswordType">
          <el-select v-model="addStudentDto.PasswordType" placeholder="请选择密码类型" filterable @change="changePasswordType"
            class="w-220">
            <el-option label="默认6个8" :value="1" />
            <el-option label="随机六位数" :value="2" />
            <el-option label="自定义" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="自定义密码:" prop="PassWord" v-show="addStudentDto.PasswordType == '3'">
          <el-input v-model="addStudentDto.Password" maxlength="20" show-word-limit class="w-220" />
        </el-form-item>
        <el-form-item label="到期时间" prop="ExpirationTime">
          <el-date-picker v-model="addStudentDto.ExpirationTime" type="datetime" placeholder="请选择到期时间" class="w-220" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="addStudent(AddStudentToRef)">保存
          </el-button>
          <el-button @click="CloseAddPanel(AddStudentToRef)">取消</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog v-model="updatePanelVisible" title="修改用户" :close-on-click-modal="false"
      @close="CloseUpdatePanel(UpdateStudentToRef)" width="30%" center>
      <el-form ref="UpdateStudentToRef" label-position="left" label-width="100px" :rules="rules" :model="updateStudentDto"
        style="max-width: 460px">
        <el-form-item label="班级名称" prop="ClassId">
          <el-select placeholder="请选择" filterable v-model="updateStudentDto.ClassId" disabled>
            <el-option v-for="item in classList" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="代表队" prop="DelegateTeamId">
          <el-select placeholder="请选择" filterable v-model="updateStudentDto.DelegateTeamId" disabled>
            <el-option v-for="item in delegateTeamList" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>

        <el-form-item label="学生账号:" prop="UserId">
          <el-input v-model="updateStudentDto.UserId" disabled />
        </el-form-item>
        <el-form-item label="密码:" prop="PassWord">
          <el-input v-model="updateStudentDto.PassWord" />
        </el-form-item>

        <el-form-item label="学生名称:" prop="UserName">
          <el-input v-model="updateStudentDto.UserName" />
        </el-form-item>
        <el-form-item label="学号:" prop="StudentNumber">
          <el-input v-model="updateStudentDto.StudentNumber" />
        </el-form-item>
        <el-form-item label="性别:" prop="Sex">
          <el-select placeholder="请选择" filterable v-model="updateStudentDto.Sex">
            <el-option v-for="item in sex" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话:" prop="TelePhone">
          <el-input v-model="updateStudentDto.TelePhone" />
        </el-form-item>
        <el-form-item label="邮箱:" prop="Email">
          <el-input v-model="updateStudentDto.Email" />
        </el-form-item>
        <el-form-item label="到期时间" prop="ExpirationTime">
          <el-date-picker v-model="updateStudentDto.ExpirationTime" type="datetime" placeholder="请选择到期时间"
            style="width: 100%" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="updateStudent(UpdateStudentToRef)">保存</el-button>
          <el-button @click="CloseUpdatePanel(UpdateStudentToRef)">取消</el-button>
        </span>
      </template>
    </el-dialog>

    <PureTableBar title="用户列表" @refresh="onSearch()" class="h100-60">
      <template #buttons>
        <p class="text-red-500 absolute left-[120px] leading-7 text-sm">注：双击查看详情</p>
        <el-button type="primary" :icon="useRenderIcon(AddFill)" @click="ShowAddStudentPanel()">
          批量添加
        </el-button>
      </template>
      <template v-slot="{ size, checkList }">
        <pure-table border align-whole="center" showOverflowTooltip table-layout="auto" :loading="loading" :size="size"
          :data="dataList" :columns="columns" :checkList="checkList" :pagination="pagination"
          :paginationSmall="size === 'small' ? true : false" @cell-dblclick="ShowUpdateStudentPanel" :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)'
          }" @size-change="handleSizeChange" @current-change="handleCurrentChange">
          <template #operation="{ row }">
            <el-button class="reset-margin" link type="primary" :size="size" :icon="useRenderIcon(EditPen)"
              @click="ShowUpdateStudentPanel(row)">
              修改
            </el-button>
            <el-button class="reset-margin" link type="danger" :size="size" @click="handleDelete(row)"
              :icon="useRenderIcon(Delete)">
              删除
            </el-button>
            <!-- <el-popconfirm title="是否确认删除?" @confirm="handleDelete(row)">
              <template #reference>
                <el-button class="reset-margin" link type="danger" :size="size" :icon="useRenderIcon(Delete)">
                  删除
                </el-button>
              </template>
            </el-popconfirm> -->
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-form-item) {
  margin-bottom: 15px;
}
</style>
